package com.pic.mgt.dao.syushin;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Map;
import java.util.Vector;

import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.RowMapper;

import com.pic.mgt.dao.BusinessTargetActionDao;
import com.pic.mgt.utils.IgxCollectionUtils;
import com.pic.mgt.utils.SqlScriptUtils;

// (送多方新增)
public final class SyushinBusinessTargetActionDao extends BusinessTargetActionDao implements
		ISyushinBusinessTargetActionDao {
	
	private final String tableName = "syushin_business_target_action";
	
	private final String insertBasicSqlScript;
	private final String findSyushinBusinessTargetActionInfoByFileNameAndApNameSqlScript;
	private final String updateCreatingTimeByFilenameAndApNameSqlScript;
	private final String updateLastTxTimeByFilenameAndApNameSqlScript;
	
	public SyushinBusinessTargetActionDao() {
		super();
		
		// 建立 Insert Basic 的 Sql Script
		this.insertBasicSqlScript = createInsertBasicSQLScript();
		
		// 建立 Find Syushin Business Target Action Info By File Name and Ap Name 的 Sql Script
		this.findSyushinBusinessTargetActionInfoByFileNameAndApNameSqlScript = createFindSyushinBusinessTargetActionInfoByFileNameAndApNameSqlScript();
		
		// 建立 Update creating time by file name 和 ap name 的 Sql Script
		this.updateCreatingTimeByFilenameAndApNameSqlScript = createUpdateCreatingTimeByFilenameAndApNameSqlScript();
		
		// 建立 Update last transaction time by file name 和 ap name 的 Sql Script
		this.updateLastTxTimeByFilenameAndApNameSqlScript = createUpdateLastTxTimeByFilenameAndApNameSqlScript();
	}
	
	private String createUpdateLastTxTimeByFilenameAndApNameSqlScript() {
		// 要更新的欄位
		Vector<String> fieldsVector = new Vector<String>();
		fieldsVector.add(_lastTxTime);
		
		return
			SqlScriptUtils.createNamedParamUpdateScriptWithTwoConditions(
														getTableName(),
														fieldsVector,
														_fileName,
														_apName);
	}
	
	private String createUpdateCreatingTimeByFilenameAndApNameSqlScript() {
		// 要更新的欄位
		Vector<String> fieldsVector = new Vector<String>();
		fieldsVector.add(_creatingTime);
		
		return
		SqlScriptUtils.createNamedParamUpdateScriptWithTwoConditions(
														getTableName(),
														fieldsVector,
														_fileName,
														_apName);
	}
	
	private String createFindSyushinBusinessTargetActionInfoByFileNameAndApNameSqlScript() {
		Vector<String> selectFieldsVector =
			new Vector<String>();

		selectFieldsVector.add(_terminalId);
		selectFieldsVector.add(_fileName);
		selectFieldsVector.add(_fileSize);
		selectFieldsVector.add(_lastTxTime);
		selectFieldsVector.add(_creatingTime);
		selectFieldsVector.add(_apName);
		
		String[] conditionsArray = {_fileName,"AND", _apName};
		
		return
		SqlScriptUtils.createNamedParamQueryScriptWithMultiConditions(
				getTableName(), selectFieldsVector, conditionsArray);
	}
	
	private String createInsertBasicSQLScript() {
		Vector<String> insertBasicFieldsVector = new Vector<String>();
		
		// 要新增的欄位
		insertBasicFieldsVector.add(_unitName);
		insertBasicFieldsVector.add(_terminalId);
		insertBasicFieldsVector.add(_fileName);
		insertBasicFieldsVector.add(_fileSize);
		insertBasicFieldsVector.add(_apName);

		return
			 SqlScriptUtils.createNamedParamInsertScript(
					 			this.tableName,
			    				insertBasicFieldsVector);
	}

	@Override
	public int insertBasic(Map<String, Object> insertValuesMap)
			throws DataAccessException {
		if (null != insertValuesMap && !insertValuesMap.isEmpty()) {
			return
				super.namedParameterJdbcTemplate.update(this.insertBasicSqlScript,
														insertValuesMap);
		}
		else {
			return 0;
		}
	}
	
	
	
	@Override
	public String getTableName() {
		return this.tableName;
	}

	@Override
	public SyushinBasicBusinessTargetActionInfoBean findSyushinBusinessTargetActionInfoByFileNameAndApName(
			String filename, String apName) throws DataAccessException {
		try {
			return
			super.namedParameterJdbcTemplate.queryForObject(
					this.findSyushinBusinessTargetActionInfoByFileNameAndApNameSqlScript,
					IgxCollectionUtils.convertToMap(_fileName, filename, _apName, apName),
					new RowMapper<SyushinBasicBusinessTargetActionInfoBean>() {
						public SyushinBasicBusinessTargetActionInfoBean mapRow(ResultSet rs, int rowNum) throws SQLException {
							SyushinBasicBusinessTargetActionInfoBean syushinBasicBusinessTargetActionInfoBean = new SyushinBasicBusinessTargetActionInfoBean();
						
							syushinBasicBusinessTargetActionInfoBean.setTerminalId(rs.getString(_terminalId));
							syushinBasicBusinessTargetActionInfoBean.setFilename(rs.getString(_fileName));
							syushinBasicBusinessTargetActionInfoBean.setFilesize(rs.getLong(_fileSize));
							syushinBasicBusinessTargetActionInfoBean.setCreatingTime(new Date(rs.getTimestamp(_creatingTime).getTime()));
							syushinBasicBusinessTargetActionInfoBean.setApName(rs.getString(_apName));
							
							Timestamp latestTxTimestamp = rs.getTimestamp(_lastTxTime);
							if (null != latestTxTimestamp) {
								syushinBasicBusinessTargetActionInfoBean.setLastTransactionTime(new Date(latestTxTimestamp.getTime()));
							}
							else {
								syushinBasicBusinessTargetActionInfoBean.setLastTransactionTime(null);
							}
							
							return syushinBasicBusinessTargetActionInfoBean;
						}
					}
				);
		} catch (EmptyResultDataAccessException e) {
			return null;
		} catch (DataAccessException e) {
			throw e;
		}
	}

	@Override
	public int updateCreatingTimeByFilenameAndApName(Timestamp creatTime,
			String fileName, String apName) throws DataAccessException {
		if (null != creatTime && null != fileName && null != apName) {
			Map<String, Object> updateValuesMap =
				IgxCollectionUtils.convertToMap(
						new Object[] {_creatingTime, creatTime,
									  "__ConditionValue1", fileName,
									  "__ConditionValue2", apName});
		
		return
			super.namedParameterJdbcTemplate.update(
							this.updateCreatingTimeByFilenameAndApNameSqlScript,
							updateValuesMap);
		}
		else {
			return 0;
		}
	}

	@Override
	public int updateLastTxTimeByFilenameAndApName(Timestamp lastTxTime,
			String filename, String apName) throws DataAccessException {
		if (null != lastTxTime && null != filename && null != apName) {
			Map<String, Object> updateValuesMap =
				IgxCollectionUtils.convertToMap(
						new Object[] {_lastTxTime, lastTxTime,
									  "__ConditionValue1", filename,
									  "__ConditionValue2", apName});
		
		return
			super.namedParameterJdbcTemplate.update(
							this.updateLastTxTimeByFilenameAndApNameSqlScript,
							updateValuesMap);
		}
		else {
			return 0;
		}
	}
}
